package com.app.twth.net;

import static com.app.twth.utils.Utils.isEmpty;

import android.app.Activity;

import com.app.twth.app.Constant;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.PdaPassword;
import com.app.twth.utils.PreferencesUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;

public class OkHttpUtils {

    Activity mContext;
    private static OkHttpUtils mInstance;

    private OkHttpUtils(Activity context) {
        this.mContext = context;
    }

    public synchronized static OkHttpUtils getInstance(Activity context) {
        if (mInstance == null) {
            mInstance = new OkHttpUtils(context);
        }
        return mInstance;
    }

    public void getPdaPassword() {
        OkGo.<BaseResponseEntity<String>>get(Constant.getPassword)
                .tag(this)
                .execute(new JsonCallBack<BaseResponseEntity<String>>() {
                    @Override
                    public void onSuccessData(Response<BaseResponseEntity<String>> response) {
                        try {
                            BaseResponseEntity<String> baseResponseEntity = response.body();
                            if (baseResponseEntity == null) {
                                return;
                            }
                            if (baseResponseEntity.getCode() == null) {
                                return;
                            }
                            if (baseResponseEntity.getCode().equals("200")) {
                                String str = response.body().data;
                                if (!isEmpty(str)) {
                                    PreferencesUtil.putString(mContext, Constant.submit_password, str);
                                    PdaPassword pdaPassword = new PdaPassword();
                                    pdaPassword.setPassword(str);
                                    EventBus.getDefault().post(pdaPassword);
                                }
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(String error) {
                    }
                });

    }

}
